package code;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import newtank.data.ToClientDataPackage;

/**
 * 向客户端发送的数据包
 * @author lijiahao
 * @since 2022/1/28
 */
public class GpcDataClientEncoder extends MessageToByteEncoder<ToClientDataPackage> {

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, ToClientDataPackage msg, ByteBuf out) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        byte[] data = mapper.writeValueAsBytes(msg);
        out.writeInt(data.length);
        out.writeBytes(data);
    }
}
